﻿<!DOCTYPE html>
<html>
	<head>
		<title>编辑模板</title>
    $css()
	    <style type="text/css">
            .flat_rgt {
                position: fixed;right: 1em;top: 5px;

            } .data-table thead td {
                background: #EEE;
                padding: 5px 0;
                text-align: center;
                border-right: solid 1px #ddd;
            }

    	    .data-table tbody td {
    	        background: #FFF;
    	        padding: 3px 0;
    	        border-right: solid 1px #ddd;
    	        border-top: solid 1px #ddd;
    	        text-align: center;
    	    }

    	    .data-table tbody input {
    	        line-height: 1.4em;
    	        width: 80%;
    	        margin: 1px auto;
    	        color: #666;
    	    }

    	    .data-table tbody input:focus {
    	        margin: 1px auto;
    	    }

    	    .data-panel .panel {
    	        overflow: auto;
    	        border-bottom: solid 1px #ddd;
    	    }
    	</style>
	</head>
<body style="overflow-y:hidden">
<div style="padding:6px 10px">
    <!--
    【当前视图目录】:<span class="txt">/templates/${tpl}/</span>&nbsp;&nbsp;<a href="?module=system&action=appconfig">设置</a>-->

    <span class="ui-button small-button" id="btn_saveNames">
        <span class=" button-inner">
            <span class="button-txt">保存名称</span>
            <a href="javascript:;"></a>
        </span>
    </span>
    
    <div class="flat_rgt">
        &nbsp;&nbsp;创建文件：<input type="text" id="tplname" class="tb_normal ui-box" style="width: 120px; line-height: 1.6em" />
        <select id="tpltype" class="tb_normal ui-box" style="width: 100px"><option value="0">页面视图</option><option value="1">部分视图</option></select>

        <span class="ui-button w50 small-button" id="createview">
            <span class=" button-inner">
                <span class="button-txt">创建</span>
                <a href="javascript:;"></a>
            </span>
        </span>

        <span class="ui-button small-button">
            <span class=" button-inner">
                <span class="button-txt">下载模板</span>
                <a href="javascript:;" class="fn" fn="download"></a>
            </span>
        </span>

    </div>
 </div>



<div class="data-panel" id="locale-data">
        <table width="100%" class="data-table" cellspacing="0" style="border-top: solid 1px #167CD2;">
            <thead id="thead">
                <tr>
                    <td class="fix-col">文件路径</td>
                    <td class="fix-col" width="">页面名称</td>
                    <td class="fix-col">操作</td>
                </tr>
            </thead>
        </table>

        <div class="panel" id="data-list">
            <table width="100%" class="data-table" cellspacing="0">
                <tbody id="tbody"></tbody>
            </table>
        </div>
</div>


<iframe id="ifr" src="about:blank" frameborder="0" scrolling="no" class="hidden"></iframe>
$js()
  <script type="text/javascript">

      /*
        ifr.src = '?module=template&action=editfile&path=' + src;


        filelist.onchange = function () {
            if (/\.css/g.test(this.value)) {
                ifr.src = '?module=file&action=editfile&path=' + this.value;
            } else {
                ifr.src = '?module=template&action=editfile&path=' + this.value;
            }
        };
        */

      function editFile(path, name) {
          var url;
          if (/\.css/g.test(path)) {
              url = '?module=file&action=editfile&path=/templates/${tpl}/' + path;
          } else {
              url = '?module=template&action=editfile&path=/templates/${tpl}/' + path;
          }
          parent.FwTab.show(name || path,url);
      }

        jr.$('createview').onclick = (function (t) {
            return function () {
                var tplname = jr.val('tplname');
                var tpltype = jr.val('tpltype');
                if (tplname != '') {
                    jr.xhr.post('?', 'module=template&action=createview&name=' + tplname + '&type=' + tpltype, function (x) {
                        if (x.indexOf('.') == -1) {
                           showMsg2(x);
                        } else {
                            location.replace('?module=template&action=edit&file=' + x);
                        }
                    });
                }
            };
        })(this);

        //下载模板
        window.Fn.download = function () {
            location.href = '?module=template&action=DownloadZip&tpl=${tpl}';
        };


        var currentType = "custom";
        var langCount = 8;
        var tby = jr.$('tbody');
        var tds = jr.$('thead').getElementsByTagName('TD');
        var rowTpl = '<tr><td class="key" width="300" field="k_{row}">{path}</td><td width="120"><input type="text" class="ui-box" field="v_{row}" value="{name}"/></td>' +
                '<td><a href="javascript:;" onclick="editFile(\'{path}\')">编辑</a></td></tr>';
        function writeToBody(d) {
            if (d == null) return;
            var html = '';
            for (var i = 0; i < d.length; i++) {
                html += jr.template(rowTpl, { row: i, path: d[i].path, name: d[i].name });
            }
            tby.innerHTML = html;
            if (d.length > 0) {
                fixTitRow();
            }
        }

        function fixTitRow() {
            var srcTds = tby.getElementsByTagName('TR')[0].getElementsByTagName('TD');
            for (var i = 0; i < tds.length; i++) {
                var width = Math.floor(srcTds[i].offsetWidth);
                if (tds.length - 1 == i) {
                    tds[i].style.width = (width + getScrollWidth()) + 'px';
                } else {
                    tds[i].style.width = width + 'px';
                    srcTds[i].style.width = width + 'px';
                }
            }
            var pl = jr.$('data-list');
            var y = document.documentElement.clientHeight;
            if (pl.scrollHeight > y - 80) {
                pl.style.height = (y - 80) + 'px';
            } else {
                pl.style.height = 'inherit';
            }
        }

        function getScrollWidth() {
            var noScroll, scroll, oDiv = document.createElement("DIV");
            oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
            noScroll = document.body.appendChild(oDiv).clientWidth;
            oDiv.style.overflowY = "scroll";
            scroll = oDiv.clientWidth;
            document.body.removeChild(oDiv);
            return noScroll - scroll;
        }

        function loadTemplateList() {
            jr.xhr.jsonPost('?module=template&action=getListJson', { tpl: '${tpl}' }, function(d) {
                writeToBody(d);
            });
        }

        loadTemplateList();
        jr.$('btn_saveNames').onclick = function() {
            var data = jr.json.toObject('data-list');
            var t = this;
            t.disabled = true;
            jr.xhr.jsonPost('?module=template&action=SaveNames&tpl=${tpl}', data, function (r) {
                t.disabled = false;
                if (r.result) {
                    jr.dialog.alert('保存成功', function () { loadTemplateList(); });
                } else {
                    jr.dialog.alert(r.message);
                }
            });
        };

        window.saveData = function () {
            jr.$('btn_saveNames').onclick();
        };

  </script>
</body>
</html>